package com.sx.manage.service.huaxia;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.sx.common.util.DateUtil;
import com.sx.common.util.json.JackJsonUtil;
import com.sx.manage.service.bank.IMBaseConsumerBankMapService;
import com.sx.manage.service.sys.IMBaseSystemConfigInfoService;
import com.sx.manage.util.HttpUtil;
import com.sx.model.constants.MSystemConstants;
import com.sx.model.manage.mybatis.form.bank.MConsumerBankMapForm;
import com.sx.model.manage.mybatis.form.sys.MSystemConfigInfoForm;
import com.sx.model.manage.mybatis.vo.consumer.MConsumerBankMapVO;
import com.sx.model.manage.mybatis.vo.huaxiabank.MLogisticsExpVO;
import com.sx.model.manage.mybatis.vo.huaxiabank.MPaywaterInfoVO;
import com.sx.model.manage.mybatis.vo.huaxiabank.MReceivePeoVO;
import com.sx.model.manage.mybatis.vo.huaxiabank.MSenderPeoVO;
import com.sx.model.manage.mybatis.vo.result.ResultObjectVO;
import com.sx.model.manage.mybatis.vo.sys.MSysConfigVO;
@Service("huaXiaExpressPrintServiceImpl")
public class HuaXiaExpressPrintServiceImpl {
	
	private Logger _log = LoggerFactory.getLogger(this.getClass());
	
	@Resource(name="mBaseSystemConfigInfoServiceImpl")
	private IMBaseSystemConfigInfoService mBaseSystemConfigInfoServiceImpl;
	@Resource(name="mBaseConsumerBankMapServiceImpl")
	private IMBaseConsumerBankMapService mBaseConsumerBankMapServiceImpl;
	//@Resource(name="payWaterService")
	//private IPayWaterServiceInterface payWaterService;
	
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public ResultObjectVO querySubOrderNo(MReceivePeoVO receiver) {
		if (null == receiver) {
			return new ResultObjectVO(false, "收件人信息错误", null, null);
		}//
		ResultObjectVO<MSysConfigVO> obj = mBaseSystemConfigInfoServiceImpl.queryByIdSystemConfig(new MSystemConfigInfoForm(MSystemConstants.LHZX_RECEIVER_INFO_));
		MSysConfigVO systemConfigVO = obj.getObjVo();
		if(null == systemConfigVO){
			return new ResultObjectVO(false, "参数配置错误", null, null);
		}
		MSenderPeoVO sender  = JackJsonUtil.jsonToJavaBean(systemConfigVO.getConfig_value(), MSenderPeoVO.class);
		MConsumerBankMapVO uvo = mBaseConsumerBankMapServiceImpl.queryConsumerBankMapByForm(new MConsumerBankMapForm(Long.valueOf(receiver.getUserId()), MSystemConstants.CONSUMER_NATURE_PERSON));
		if (null == uvo) {
			return new ResultObjectVO(false,null,"摊位号信息错误", null);
		}
		//组装数据
		MPaywaterInfoVO paywaterInfo = new MPaywaterInfoVO();
		//寄件人
		paywaterInfo.setSender(sender);
		//收件人
		paywaterInfo.setReceiver(receiver);
		paywaterInfo.setAccountNo(uvo.getAccount_no());
		String reqJson = JackJsonUtil.objToJson(paywaterInfo);
		//3.发送请求获取快递单号
		
		ResultObjectVO objr = mBaseSystemConfigInfoServiceImpl.queryByIdSystemConfig(new MSystemConfigInfoForm(MSystemConstants.SUB_ORDERNO_URL_));
		MSysConfigVO sVO = (MSysConfigVO)objr.getObjVo(); 
		if(null == sVO){
			return new ResultObjectVO(false, "发送请求配置错误", null, null);
		}
		_log.info("宅急送下订单地址："+sVO.getConfig_value());
		_log.info("宅急送下单数据："+reqJson);
		//发送 下单请求
		String jsonStr = HttpUtil.post(sVO.getConfig_value(),reqJson);
		_log.info("创建订单返回信息:"+jsonStr);
		//4.回显单号-打印
		MLogisticsExpVO pvo = (MLogisticsExpVO)JackJsonUtil.jsonToJavaBean(jsonStr, MLogisticsExpVO.class);
		String time = pvo.getData().getSubtime();
		if (!pvo.isSuccess()) {
			_log.info("创建订单返回失败原因："+pvo.getMsg());
			ResultObjectVO resultObjectVO = new ResultObjectVO();
			resultObjectVO.setSuccess(false);
			resultObjectVO.setMsg(pvo.getMsg());
			return  resultObjectVO;
		}
		
//		CPayWaterRecordEntity payWater = payWaterService.findPayWaterById(receiver.getId());
//		if (null == payWater) {
//			return new ResultObjectVO(false, "获取信息失败", null, payWater);
//		}
//		payWater.setNumber(pvo.getData().getMailNo());
//		
//		//更新订单信息=订单号
//		payWaterService.updatePayWater(payWater);
//		pvo.getData().setSubtime(DateUtil.stringOrFormat(time.substring(0, 8)));
		return new ResultObjectVO(true, null, "获取信息成功", pvo);
	}
	
	/**
	 * 组装 MSenderPeoVO
	 * @param sender
	 * @return
	 */
	public MSenderPeoVO create_MPaywaterInfo(MSenderPeoVO sender){
		//组装数据
		MSenderPeoVO mSenderPeoVO = new MSenderPeoVO();
		mSenderPeoVO.setName(sender.getName());
		mSenderPeoVO.setPostCode(sender.getPostCode());
		mSenderPeoVO.setPhone(sender.getPhone());
		mSenderPeoVO.setMobile(sender.getMobile());
		mSenderPeoVO.setProv(sender.getProv());
		mSenderPeoVO.setCity(sender.getCity());
		mSenderPeoVO.setDistrict(sender.getDistrict());
		mSenderPeoVO.setAddress(sender.getAddress());
		return mSenderPeoVO;
	}
	
	/**
	 * 查询订单信息
	 * @param mailNo
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ResultObjectVO queryPrintExpress(String mailNo) {
		if (null == mailNo) {
			return new ResultObjectVO(false, "订单号不能为空", null, null);
		}
		ResultObjectVO obj = mBaseSystemConfigInfoServiceImpl.queryByIdSystemConfig(new MSystemConfigInfoForm(MSystemConstants.SUB_PRINTINGINFO_URL_));
		MSysConfigVO svO = (MSysConfigVO)obj.getObjVo();
		if(null == svO){
			return new ResultObjectVO(false, "发送请求配置错误", null, null);
		}
		String url = svO.getConfig_value();
		_log.info("==MHuaXiaExpressPrintServiceImpl==queryPrintExpress==url:"+url);
		String jsonStr = HttpUtil.send_Get(url,mailNo);
		_log.info("==MHuaXiaExpressPrintServiceImpl==queryPrintExpress==jsonStr:"+jsonStr);
		//4.回显单号-打印
		MLogisticsExpVO pvo = JackJsonUtil.jsonToJavaBean(jsonStr, MLogisticsExpVO.class);
		if (null == pvo) {
			return new ResultObjectVO(false, "没有你要查询的数据", null, null);
		}
		String time = pvo.getData().getSubtime();
		
		pvo.getData().setSubtime(DateUtil.stringOrFormat(time.substring(0, 8)));
		return new ResultObjectVO(true, null,"获取信息成功", pvo);
	}

}
